package com.supermap.business.advisoryinfo;

import com.alibaba.dubbo.config.ReferenceConfig;
import com.alibaba.dubbo.config.RegistryConfig;
import com.alibaba.dubbo.config.annotation.Reference;
import com.supermap.dataservice.util.JSONUtil;
import com.supermap.dataservice.util.Table;
import com.supermap.dubbo.api.advisoryinfo.AdvisoryinfoService;
import com.supermap.dubbo.api.advisoryinfo.entity.AdvComment;
import com.supermap.dubbo.api.advisoryinfo.entity.Advisoryinfo;
import com.supermap.dubbo.api.advisoryinfo.entity.Advpraise;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;

/**
 * @Author:langhongshuai
 * @Description:咨询接口层
 * @Date:18/1/29 上午9:27
 */
@Controller
@RequestMapping("AdvisroyInfoController")
public class AdvisroyInfoController {

    /**
     * 使用指定类初始化日志对象，在日志输出的时候，可以打印出日志信息所在类
     */
    protected static final Logger logger = LoggerFactory.getLogger(AdvisroyInfoController.class);

    @Autowired
    private RegistryConfig registryConfig;

    @Reference(timeout = 10000)
    private AdvisoryinfoService advisoryinfoService;

    /**
     * 通过用户id和区域code获得用户的咨询信息
     */
    @RequestMapping("/getAdvisoryInfoByUIdArea")
    @ResponseBody
    public JSONUtil getAdvisoryInfoByUIdArea(HttpServletRequest request) {
        ReferenceConfig referenceConfig = new ReferenceConfig();
        RegistryConfig registryConfig = new RegistryConfig();
        registryConfig.setAddress("");
        referenceConfig.setRegistry(new RegistryConfig());
        String userId = request.getParameter("userId");
        String areaCode = request.getParameter("areaCode");
        return advisoryinfoService.getAdvisoryInfoByUIdArea(userId, areaCode);
    }
    /**
     * 上传咨询
     */
    @RequestMapping(value = "/addAdvisory", method = RequestMethod.POST)
    @ResponseBody
    public JSONUtil addAdvisory(@RequestBody HashMap<String, String> data) {
        String userId = data.get("userId");
        String areaCode = data.get("areaCode");
        String question = data.get("question");
        String subjectId = data.get("subjectId");
        String farmlandid = data.get("farmlandId");
        String latitude = data.get("latitude");
        String longitude = data.get("longitude");
        String imgUrl = data.get("imgUrl");
        String development = data.get("development");
        String expertId = data.get("expertId");


        logger.debug("AdvisroyInfoController===>>>addAdvisory====>>> userId" + userId);
        logger.debug("AdvisroyInfoController===>>>addAdvisory====>>> areaCode" + areaCode);
        logger.debug("AdvisroyInfoController===>>>addAdvisory====>>> question" + question);
        logger.debug("AdvisroyInfoController===>>>addAdvisory====>>> farmlandId" + farmlandid);
        logger.debug("AdvisroyInfoController===>>>addAdvisory====>>> latitude" + latitude);
        logger.debug("AdvisroyInfoController===>>>addAdvisory====>>> longitude" + longitude);
        logger.debug("AdvisroyInfoController===>>>addAdvisory====>>> imgUrl" + imgUrl);
        logger.debug("AdvisroyInfoController===>>>addAdvisory====>>> subjectId" + subjectId);
        Advisoryinfo advisoryinfo = new Advisoryinfo();
        advisoryinfo.setUserId(Integer.parseInt(userId));
        advisoryinfo.setAreacode(areaCode);
        advisoryinfo.setQuestion(question);
        advisoryinfo.setSubjectid(subjectId);
        advisoryinfo.setDevelopment(development);
        advisoryinfo.setExpertId(expertId);
        //无关农田的咨询，不需要上传农田Id
        if (farmlandid != null) {
            advisoryinfo.setFarmlandid(Integer.parseInt(farmlandid));
        }
        advisoryinfo.setLat(latitude);
        advisoryinfo.setLon(longitude);
        advisoryinfo.setImgUrl(imgUrl);
        advisoryinfo.setUploadTime(new Date());
        return advisoryinfoService.addAdvisory(advisoryinfo);
    }
    /**
     * 查询所有咨询信息
     */
    @RequestMapping("/getAdvisoryInfo")
    @ResponseBody
    public JSONUtil getAdvisoryInfo(HttpServletRequest request){
        return advisoryinfoService.getAdvisoryInfo();
    }
    /**
     * 根据咨询ID得到对应总赞数
     * @return
     */
    @RequestMapping("/getAdvpraiseCount")
    @ResponseBody
    public int getAdvpraiseCount(HttpServletRequest request){
        String advInfoId= request.getParameter("advInfoId");
        return advisoryinfoService.getAdvpraiseCount(Integer.valueOf(advInfoId));
    }
    /**
     * 判断该用户是否点过赞
     * @param request
     * @return
     */
    @RequestMapping("/getAdvpraise")
    @ResponseBody
    public boolean getAdvpraise(HttpServletRequest request){
        String userId= request.getParameter("userId");
        String advInfoId= request.getParameter("advInfoId");
        return advisoryinfoService.getAdvpraise(Integer.valueOf(userId),Integer.valueOf(advInfoId));
    }

    /**
     * 添加点赞
     * @return
     */
    @RequestMapping("/addAdvpraise")
    @ResponseBody
    public boolean addAdvpraise(HttpServletRequest request, Advpraise advpraise){
        return advisoryinfoService.addAdvpraise(advpraise);
    }

    /**
     * 增添评论
     * @param request
     * @param advComment
     * @return
     */
    @RequestMapping("/addAdvcomment")
    @ResponseBody
    public boolean addAdvcomment(HttpServletRequest request, AdvComment advComment){
        String uid=request.getParameter("uid");
        return advisoryinfoService.addAdvcomment(advComment,uid);
    }

    /**
     * 根据咨询ID 得到对应下的所有评论
     * @param advInfoId
     * @return
     */
    @RequestMapping("/getAdvCommentListByAdvInfoId")
    @ResponseBody
    public JSONUtil getAdvpraiseListByAdvInfoId(int advInfoId){
        return advisoryinfoService.getAdvCommentListByAdvInfoId(advInfoId);
    }

    /**
     * 删除咨询
     * @param request
     * @return
     */
    @RequestMapping("/delAdvisory")
    @ResponseBody
    public JSONUtil delAdvisory(HttpServletRequest request){
        String advisoryId = request.getParameter("advisoryId");
        return advisoryinfoService.delAdvisoryInfo(Integer.parseInt(advisoryId));
    }

    /**
     * 根据作物ID和区域编码获取咨询
     * @param request
     * @return
     */
    @RequestMapping("/getAdvinfoByAreacodeandCropid")
    @ResponseBody
    public JSONUtil  getAdvinfoByAreacodeandCropid(HttpServletRequest request){
        String areacode = request.getParameter("areacode");
        String subjectid = request.getParameter("cropid");
        return advisoryinfoService.getAdvinfoByAreacodeandCropid(areacode,subjectid);
    }
    /**
     * 根据用户ID获取该咨询的评论 点赞数
     * @param request
     * @return
     */
    @RequestMapping("/getAdvAllInfoByUserIdorAreaCode")
    @ResponseBody
    public Table getAdvAllInfoByUserIdorAreaCode(HttpServletRequest request,Table table){
        String userId=request.getParameter("userId");
        String areaCode=request.getParameter("areaCode");
        String subjectId=request.getParameter("subjectId");
        String expertId=request.getParameter("expertId");
        JSONUtil jsonUtil = advisoryinfoService.getAdvAllInfoByUserIdorAreaCode(table,userId,areaCode,subjectId,expertId);
        table =(Table)jsonUtil.getData();
        table.setMsg(jsonUtil.getMsg());
        table.setCode(jsonUtil.getCode());
        return table;
    }

    /**
     * 根据userId和subjectid查询相关咨询
     * @param request
     * @return
     */
    @RequestMapping("/getAdvByuserIdAndSubject")
    @ResponseBody
    public JSONUtil getAdvByuserIdAndSubject(HttpServletRequest request){
        String userId=request.getParameter("userId");
        String subjectid=request.getParameter("subjectid");
        return advisoryinfoService.getAdvByuserIdAndSubject(userId,subjectid);
    }
}
